<template>
	<view>

		<!-- 日历组件 -->
		<view class="calendarBox">
			<Calendar ref="calendar" :dotList="dotList" :error="isError" @year="getYear" @month="getMonth" @day="getDay"
				@weekMode="getWeekMode" @selected-change="dateChange" parent="keepHome">
			</Calendar>
		</view>

		<!-- 间隔槽 -->
		<u-gap height="20" bg-color="#F5F5F5"></u-gap>

		<!-- 选择工友 -->
		<view class="recordBox" v-if="defaultWorksite.role>1 && noWorkerTotal>0">
			<view class="recordHead_con">
				<view class="recordNum">
					选择工友 · <text>{{noWorkerTotal}}</text>
				</view>
				<view class="recordAll" @click="noSelectAll">
					<checkbox color="#327FE8" :checked="noSelectAllStatus" />
					<text>全选</text>
				</view>
			</view>
			<view class="recordBody_con">
				<view class="workerList_con">
					<view class="workerItem" v-for="(item,index) in noWorkerList" :key="index"
						@click="noWorkerSelect(index)">
						<view class="workerAvatar">
							{{item.userInfo.name_str}}
							<view class="workerTick" v-if="item.selected==1"></view>
							<view class="workerSet" v-if="item.num > 0 || item.num=='休'">
								{{item.num}}{{item.num=='休'?'':'工'}}
								<text v-if="item.overtime_days > 0 || item.overtime_hour > 0">/</text>
								{{item.overtime_days > 0 ? item.overtime_days : ''}}
								{{item.overtime_hour > 0 ? item.overtime_hour : ''}}
							</view>
						</view>
						<view class="workerName">{{item.userInfo.name_str2}}</view>
					</view>
				</view>
			</view>
		</view>

		<!-- 间隔槽 -->
		<u-gap height="20" bg-color="#F5F5F5"></u-gap>

		<!-- 选项卡 -->
		<view class="recordTab_box">
			<view class="recordTab_con">
				<view :class="'recordTabItem '+(recordTabIndex==1?'active':'')" @click="tabClick(1)">
					<text>记工</text>
				</view>
				<view :class="'recordTabItem '+(recordTabIndex==2?'active':'')" @click="tabClick(2)">
					<text>记工流水</text>
				</view>
			</view>
		</view>

		<!-- 记工操作 -->
		<view class="handleList_box" v-if="recordTabIndex==1">
			<view class="handleList_con">
				<view class="handleLabel">类型：</view>
				<view class="handleList">
					<view :class="'handleItem '+(selectType==1?'handleActive':'')" @click="typeClick(1)">点工
					</view>
					<view :class="'handleItem '+(selectType==2?'handleActive':'')" @click="typeClick(2)">包工
					</view>
				</view>
			</view>
			<view class="handleList_con" v-if="selectType==1">
				<view class="handleLabel">上班：</view>
				<view class="handleList">
					<view :class="'handleItem '+(workType==1?'handleActive':'')" @click="workSelect(1)">{{defaultWorksite.work_type==0?'1':'8'}}{{defaultWorksite.work_type==0?'工':'时'}}
					</view>
					<view :class="'handleItem '+(workType==2?'handleActive':'')" @click="workSelect(2)">{{defaultWorksite.work_type==0?'0.5':'4'}}{{defaultWorksite.work_type==0?'工':'时'}}
					</view>
					<view :class="'handleItem '+(workType==3?'handleActive':'')" @click="workSelect(3)">休息
					</view>
					<view :class="'handleItem '+(workType==4?'handleActive':'')" style="flex: 2;"
						@click="workSelect(4)">
						<input type="text" :value="workInput" @input="workNumInput" @blur="workNumBlur"
							placeholder="自定义">
					</view>
				</view>
			</view>
			<view class="handleList_con" v-if="selectType==1">
				<view class="handleLabel">加班：</view>
				<view class="handleList">
					<view :class="'handleItem '+(selectOver==1?'handleActive':'')" @click="openWorkPopup(1)">无加班
					</view>
					<view :class="'handleItem '+(selectOver==2?'handleActive':'')" style="flex: 2;"
						@click="openWorkPopup(2)" v-if="defaultWorksite.work_type==0">
						<view class="overInput">{{(selectOver==2 && overWork>0) ? overWork+'工' : '按工算'}}</view>
						<view class="overLine"></view>
						<image src="/static/keep/editBtn.png" mode="" class="overIcon"></image>
					</view>
					<view :class="'handleItem '+(selectOver==3?'handleActive':'')" style="flex: 2;"
						@click="openWorkPopup(3)">
						<view class="overInput">{{(selectOver==3 && overWork>0) ? overWork+'时' : '按小时'}}</view>
						<view class="overLine"></view>
						<image src="/static/keep/editBtn.png" mode="" class="overIcon"></image>
					</view>
				</view>
			</view>
			<view class="handleList_con" v-if="selectType==2">
				<view class="handleLabel">金额：</view>
				<view class="handleList">
					<view class="baoCon">
						<input type="decimal" :value="workInput" @input="workNumInput" @blur="workNumBlur"
							placeholder="0.00" placeholder-class="placeholderColor">
						<text>元/人</text>
					</view>
				</view>
			</view>
			<view class="handleList_con">
				<view class="handleLabel">备注：</view>
				<view class="handleList">
					<view class="textareaCon">
						<textarea 
							:value="remark" 
							@input="remarkIn" 
							placeholder="请输入备注"
							placeholder-class="placeholderColor" 
							auto-blur="true" 
							cursor-spacing="100"
							></textarea>
					</view>
				</view>
			</view>
			<view class="handleList_con">
				<view class="handleLabel">照片：</view>
				<view class="handleList">
					<view class="uploadList">
						<view class="uploadItem" v-for="(item, index) in imgList" :key="index"
							v-if="imgList.length >0">
							<image :src="item.url" mode="aspectFill" @click="previewImg(item.url)"></image>
							<view class="delPic" @click="deleteImg(index)">
								<u-icon name="minus-circle-fill" color="#fa3534" size="50"></u-icon>
							</view>
						</view>
						<view class="uploadItem uploadBtn" @click="openUpload()">
							<u-icon name="camera" color="#BCBCBC" size="50"></u-icon>
						</view>
					</view>
					<!-- <view class="photoCon">
						<image :src="cameraUrl" alt="" v-if="cameraUrl" @click="previewImg(cameraUrl)"></image>
						<image src="/static/camera.png" alt="" v-else @click="openUpload"></image>
						<text @click="deleteImg(0)">删除</text>
					</view> -->
				</view>
			</view>
			<!-- #ifdef APP-PLUS -->
			<view class="addressBox">
				记工位置：{{address}}
			</view>
			<!-- #endif -->
			
			<!-- 完成记工 -->
			
			<view class="handlePost_con ">
				<view class="completeBtn" @click="openUserPop">完成记工</view>
			</view>
			
			<u-gap height="100" bg-color="#fff"></u-gap>
			
			
			
		</view>

		<!-- 记工流水 -->
		<view class="recordHistory_box" v-if="recordTabIndex==2">
			<view class="historyItem">
				<view class="historyHead_con">
					<view class="historyDate">{{extendData.time_data}}</view>
				</view>
				<view class="recordList_con">
					<view class="recordItem" v-for="(item,index) in recordList" :key="index" @click="recordEdit(item.id)">
						<view class="recordAvatar" >
							<image :src="item.userInfo.header_img" mode=""></image>
						</view>
						<view class="recordCon" >
							<view class="recordName">
								{{item.userInfo.name}}
								<text class="modify" v-if="item.is_edit==1">有修改</text>
								<text class="recorder" v-if="item.add_uase_name">(记录人:{{item.add_uase_name}})</text>
							</view>
							<view class="recordType">
								<text v-if="item.work_type==0">点工</text>
								<text v-if="item.work_type==1">包工</text>
								<text style="margin: 0 10rpx;">|</text>
								{{item.add_time_str}}
							</view>
							<!-- <view class="recordNote" v-if="item.remark">{{item.remark}}</view> -->
						</view>
						<view class="recordWork" v-if="item.num>0 || item.price>0 || item.overtime_hour>0">
							<view class="recordMoney" v-if="item.work_type==0">{{item.num}}{{item.work_class_type_str}}
							</view>
							<view class="recordMoney" v-if="item.work_type==1">
								{{item.price}}元
							</view>
							<view class="recordDay" v-if="item.overtime_hour>0">
								<text>{{item.overtime_hour}}</text>小时
							</view>
						</view>
						<view class="recordWork" v-if="item.num==0 && item.price==0 && item.overtime_hour==0"
							@click="recordEdit(item.id)">
							休息
						</view>
						<view class="recordEdit" >
							修改
						</view>
					</view>
				</view>
			</view>

			<!-- 空数据 -->
			<view class="dataEmpty_con" v-if="recordList.length==0">
				<u-empty></u-empty>
			</view>
		</view>

		

		<!-- 上传图片组件 -->
		<Upload v-if="isUpload" type="jigong" time="date" @getImgs="getImgsFunc"></Upload>
		<uploadCamera v-if="isUploadCamera" type="anxinji" time="date" @getImgs="getImgsFunc"></uploadCamera>

		<!-- 修改记工组件 -->
		<EditKeep ref="editKeep" :logIds="logId"></EditKeep>

		<!-- 选择工时组件 -->
		<WorkHour ref="workHour"></WorkHour>

	</view>
</template>

<script>
	import Calendar from "@/components/calendar/calendar.vue"
	import EditKeep from "@/components/keep/keepEdit.vue"
	import WorkHour from "@/components/keep/workHour.vue"
	import Upload from '@/components/upload/uploadAlbum.vue';
	import uploadCamera from '@/components/upload/uploadCamera.vue';

	export default {
		components: {
			Calendar,
			EditKeep,
			WorkHour,
			Upload,
			uploadCamera,
		},
		data() {
			return {
				// 默认工地信息
				defaultWorksite: JSON.parse(uni.getStorageSync('defaultWorksite')),

				/**
				 * 日历
				 */
				currentYear: '', // 年
				currentMonth: '', // 月
				currentDay: '', // 日
				weekMode: '', // 模式
				dotList: [], // 日历数据
				date: '', // 选择的日期

				// 未记工的工人
				noWorkerList: [],
				noWorkerTotal: 0,
				noSelectNum: 0,
				noSelectAllStatus: false,

				// 记工的数据
				recordList: [],
				recordTotal: 0,
				totalTodayMoney: 0,
				totalTodayNum: 0,

				// 是否有偏差
				isError: false,
				userInfo: '',

				studyList: [],
				studyIndex: 0,
				// 引导弹窗
				guideShow: false,

				// 选项卡
				recordTabIndex: 1,
				
				// 记工参数
				selectType: 1,
				workType: 0, //上班
				workNum: -1, //提交的工
				workInput: '', //输入的工
				overWork: 0, //加班
				selectOver: 1,
				remark: '',
				
				// 上传图片
				isUpload: false,
				imgList: [],
				isUploadCamera: false,
				imgType: 'album',

				userBoxShow: false,
				logId: "0",

				extendData: [], //扩展数据
				address: uni.getStorageSync('address'),
				
				textareaInput:false,
				isAddWork:true,
			}
		},
		created() {
			var _this=this;
			this.date = this.$util.nowDate();
			
		},
		onLoad(option) {
			if (option.type == 'log') {
				this.recordTabIndex = 2;
			}
		},
		onShow() {
			var _this = this;
			
			var imgCamera = uni.getStorageSync('imgCamera');
			if (imgCamera) {
				_this.imgList = _this.imgList.concat(JSON.parse(imgCamera));
				uni.setStorageSync('imgCamera', '');
			}
			
			setTimeout(function(){
				_this.getCheckCompare();
			},1500)
			_this.getDotList();
			
			_this.getRecordList();
			//工人班数据重新加载
			if(_this.defaultWorksite.role==1){
				_this.getNoWorker();
			}
			
			
			
			
			_this.dotList=uni.getStorageSync('dotList');
			
			_this.$nextTick(() => {
				var date = new Date(); // 获取现在时间
				var nowYear = date.getFullYear() // 现在年份
				var nowMonth = date.getMonth() + 1; // 现在月份
				var nowDate = date.getDate() // 现在日期
				if (_this.currentYear) {
					if (_this.currentYear == nowYear && _this.currentMonth == nowMonth && _this
						.currentDay == nowDate) {
						_this.$refs.calendar.initDate();
					} else {
						if (_this.weekMode) {
							const d = new Date(_this.currentYear, _this.currentMonth - 1, _this
								.currentDay);
							_this.$refs.calendar.initDate(d);
						} else {
							const d = new Date(_this.currentYear, _this.currentMonth - 1, 1);
							_this.$refs.calendar.initDate(d);
						}
					}
				} else {
					_this.$refs.calendar.initDate();
				}
				this.$forceUpdate();
			})
			
		},
		onReady() {
			/**
			 * 获取首页的缓存
			 */
			var _this = this;
			// 未记工的工人
			var noWorkerList = uni.getStorageSync('noWorkerList');
			if (noWorkerList) {
				_this.noWorkerList = JSON.parse(noWorkerList);
			}
			_this.getNoWorker();
			
			// #ifdef APP-PLUS
			_this.getAddress();
			// #endif
			// #ifndef MP-WEIXIN
			_this.getLocation();
			// #endif
			
			
			

		
		},
		onHide() {
			this.noSelectAllStatus = false;
		},
		methods: {
			
			focusTextarea(){
				this.textareaInput=true;
			},
			blurTextarea(){
				this.textareaInput=false;
			},
			//键盘拉起
			keyboardheightchange(e){
				console.log(e)
			},
			toRemark(){
				uni.navigateTo({
					url:'/pagesOther/user/report'
				})
			},
			//备注输入
			remarkIn(e) {
				var _this = this;
				this.remark = e.detail.value
			},
			
			getAddress() {
				let _this = this;
				uni.getLocation({
					type: 'gcj02',
					geocode: true,
					isHighAccuracy: true,
					success: (res) => {
						var address = res.address.city + res.address.district + res.address.street ;
						uni.setStorageSync('address', address);
						uni.setStorageSync('longitude', res.longitude);
						uni.setStorageSync('latitude', res.latitude);
						_this.address=address;
					},fail: function(res) {
						
					}
				});
			},
			
			getLocation() {
				// console.log(mapKey);
				let _this = this;
				var mapKey = uni.getStorageSync('mapKey');
				wx.getLocation({
					type: 'wgs84',
					// geocode: true,
					success: function(res) {
						_this.longitude = res.longitude;
						_this.latitude = res.latitude;
						// 设置经纬度缓存
						uni.setStorageSync('longitude', res.longitude);
						uni.setStorageSync('latitude', res.latitude);
						// 引入腾讯地图SDK核心类
						var QQMapWX = require('@/util/qqmap-wx-jssdk.min.js');
						var qqmapsdk = new QQMapWX({
							key: mapKey,
						});
						// 根据经纬度获取所在位置
						qqmapsdk.reverseGeocoder({
							location: {
								longitude: res.longitude,
								latitude: res.latitude,
							},
							success: function(res) {
								// 设置城市缓存
								uni.setStorageSync('province', res.result.ad_info.province);
								uni.setStorageSync('city', res.result.ad_info.city);
								uni.setStorageSync('area', res.result.ad_info.district);
								uni.setStorageSync('address', res.result.formatted_addresses
									.recommend);
									
								_this.address=res.result.formatted_addresses.recommend;
							}
						});
					},
					fail: function(res) {}
				});
			},

			

			// 修改记工数据
			recordEdit(id) {
				var _this = this;
				_this.logId = id
				uni.navigateTo({
					url: '/pages/keep/keepEdit?id=' + id
				})
			},

		
			// 记工提交
			addWorkDoing() {
				var _this = this;
				uni.showLoading({
					title: '记工中'
				});
				if(!_this.isAddWork){
					return;
				}
				_this.isAddWork=false;
				
				var noWorkerList = _this.noWorkerList;
				var addFormData = [];
				var workerIds = '';
				for (var i = 0; i < noWorkerList.length; i++) {
					if (noWorkerList[i].selected > 0 && (noWorkerList[i].num > 0 || noWorkerList[i].num == '休')) {
						_this.noWorkerList[i].selected = 10;
						var addData = noWorkerList[i];
						// 点工
						var para = {
							work_type: addData.work_type,
							member_id: addData.id,
							num: addData.num,
							overtime_days: addData.overtime_days,
							overtime_hour: addData.overtime_hour,
							price: 0,
						}
						if (addData.work_type == 1) {
							para['num'] = 0;
							para['price'] = addData.num;
						}
						addFormData[addFormData.length] = para


						if (workerIds) {
							workerIds = workerIds + ',' + addData.id;
						} else {
							workerIds = addData.id;
						}
					}
				}
				//这个地方处理记工点不了的bug
				if(addFormData.length==0 && _this.defaultWorksite.role==1){
					
					var addData = noWorkerList[0];
					// 点工
					var para = {
						work_type: addData.work_type,
						member_id: addData.id,
						num: addData.num,
						overtime_days: addData.overtime_days,
						overtime_hour: addData.overtime_hour,
						price: 0,
					}
					if (addData.work_type == 1) {
						para['num'] = 0;
						para['price'] = addData.num;
					}
					addFormData[0] = para
					
					workerIds=addData.id;
					
				}
				_this.workerIds = workerIds;
			
				// 上传图片
				let imgList = _this.imgList;
				let imgIds = [];
				for (var i = 0; i < imgList.length; i++) {
					imgIds.push(imgList[i].imgID)
				}

				var postData = {
					project_id: _this.defaultWorksite.project_id,
					address: uni.getStorageSync('address'),
					lng: uni.getStorageSync('longitude'),
					lat: uni.getStorageSync('latitude'),
					imgs: imgIds,
					remark: _this.remark,
					add_date: _this.date,
					data: JSON.stringify(addFormData),
				}


				_this.$u.post('/api/project.work/add_do2', postData).then(res => {
					_this.getDotList();
					_this.getNoWorker();
					_this.getRecordList();
					_this.userBoxShow = false;
					_this.isAddWork=true;
					uni.hideLoading()
					
					uni.showModal({
						content: '记工成功',
						showCancel: false,
						success: () => {
							_this.recordTabIndex = 2;
							
							_this.clearData();

							// 开始处理报表差异
							_this.updateCompare();
						}
					})
				}).catch(res => {
					console.log(res.errMsg);
					// uni.showModal({
					// 	title:'提示',
					// 	content:JSON.stringify(addFormData),
					// 	showCancel:false,
					// })
					
					_this.isAddWork=true;
					
					_this.$u.post('/api/project.work/userError', {
						'text':JSON.stringify(postData)
					}).then(res => {
					})
					uni.showModal({
						title:'提示',
						content:'您当前网络不稳定，为了您的数据安全，请重新记工',
						showCancel:false,
						success() {
							uni.navigateBack()
						}
					})
					setTimeout(function(){
						uni.hideLoading()
					},1000)
					_this.getNoWorker();
				})
			},

			// 更新对比数据
			updateCompare() {
				var _this = this;
				_this.$u.post('/api/project.compare/projectCompare', {
					project_id: _this.defaultWorksite.project_id,
					type: '1', //1记工、2记账
					add_date: _this.date,
					member_id: _this.workerIds,
				}).then(res => {
					console.log(res)
					console.log("==更新对比数据==")
					_this.getCheckCompare()
				}).catch(res => {
					console.log(res);
				})
			},

			

			// 打卡记工弹出框
			openUserPop() {
				var _this = this;
				var noWorkerList = _this.noWorkerList;
				var isUser = 0;
				var isNum = 0;
				
				
				
				
				if (_this.defaultWorksite.role > 1) {
					
					
					for (var i = 0; i < noWorkerList.length; i++) {
						if (noWorkerList[i].selected > 0) {
							isUser++;
							if (noWorkerList[i].num > 0 || noWorkerList[i].num == '休') {
								isNum++;
							}
						}
					}
				} else {
					
					if(noWorkerList.length==0){
						uni.showModal({
							title: '温馨提示',
							content: '今天已完成记工了',
							showCancel: false
						})
						return;
					}
					if (_this.workType == 1 || _this.workType == 2) {
						noWorkerList[0].num = _this.workNum;
					}
					if (_this.workType == 3) {
						noWorkerList[0].num == '休'
					}
					if (_this.workType == 4) {
						noWorkerList[0].num == _this.workInput
					}
					if (_this.selectOver == 0) {
						noWorkerList[0].overtime_days = 0;
						noWorkerList[0].overtime_hour = 0;
					} else if (_this.selectOver == 1) {
						noWorkerList[0].overtime_days = _this.overWork;
						noWorkerList[0].overtime_hour = 0;
					} else {
						noWorkerList[0].overtime_hour = _this.overWork;
						noWorkerList[0].overtime_days = 0;
					}
					noWorkerList[0].selected = 1;
					isUser = 1;
					if (_this.workType > 0) {
						isNum = 1;
					}
				}

				if (isUser == 0) {
					uni.showModal({
						title: '温馨提示',
						content: '未选择员工',
						showCancel: false
					})
					return;
				}
				if (isNum == 0) {
					uni.showModal({
						title: '温馨提示',
						content: '未填写上班时长',
						showCancel: false
					})
					return;
				}

				// 直接提交，不显示确认框
				_this.addWorkDoing(0);
			},

			// 选项卡切换
			tabClick(val) {
				this.recordTabIndex = val;
			},

			// 选择记工类型
			typeClick(type) {
				this.selectType = type;
			},

			// 选择上班类型
			workSelect(type) {
				this.workType = type;
				var projectType=this.defaultWorksite.work_type
				var workNum = 1;
				if (type == 1) {
					if(projectType==1){
						workNum = 8;
					}else{
						workNum = 1;
					}
					//workNum = 1;
				}
				if (type == 2) {
					// workNum = 0.5;
					if(projectType==1){
						workNum = 4;
					}else{
						workNum = 0.5;
					}
				}
				if (type == 3) {
					workNum = '休';
				}
				this.workNum = workNum;
				if (type < 4) {
					this.workInput = '';
				}
				var noWorkerList = this.noWorkerList;
				for (var i = 0; i < noWorkerList.length; i++) {
					if (noWorkerList[i].selected == 1) {
						noWorkerList[i].num = workNum;
						// noWorkerList[i].selected=2;
						noWorkerList[i].overtime_days = 0;
						noWorkerList[i].overtime_hour = 0;
						this.overWork = 0;
					}
				}
				this.noWorkerList = noWorkerList;
			},

			// 自定义工输入
			workNumInput(e) {
				this.workNum = e.detail.value;
				this.workInput = e.detail.value;
			},
			workNumBlur() {
				var noWorkerList = this.noWorkerList;
				for (var i = 0; i < noWorkerList.length; i++) {
					if (noWorkerList[i].selected == 1) {
						noWorkerList[i].num = this.workNum;
						if (this.selectType == 2) { //这个是包工的输入
							noWorkerList[i].overtime_days = 0;
							noWorkerList[i].overtime_hour = 0;
							noWorkerList[i].work_type = 1;
						}
					}
				}
				console.log(noWorkerList)
				this.noWorkerList = noWorkerList;
			},

			// 选择日期
			openWorkPopup(type) {
				this.selectOver = type;
				//无加班
				if (type == 1) {
					var noWorkerList = this.noWorkerList;
					for (var i = 0; i < noWorkerList.length; i++) {
						if (noWorkerList[i].selected == 1) {
							noWorkerList[i].overtime_days = 0;
							noWorkerList[i].overtime_hour = 0;
						}
					}
					this.noWorkerList = noWorkerList;
					this.overWork = 0;
					return
				}
				this.$refs.selectName = this.overWork;
				this.$refs.workHour.workShow = true;
				this.$refs.workHour.doingType = type;
			},

			// 弹出框关闭处理
			overWorkUpdate() {
				var _this = this;
				var noWorkerList = _this.noWorkerList;
				for (var i = 0; i < noWorkerList.length; i++) {
					if (noWorkerList[i].selected == 1) {
						if (_this.selectOver == 2) {
							noWorkerList[i].overtime_days = _this.overWork;
							noWorkerList[i].overtime_hour = 0;
						} else if (_this.selectOver == 3) {
							noWorkerList[i].overtime_hour = _this.overWork;
							noWorkerList[i].overtime_days = 0;
						}
					}
				}
				_this.noWorkerList = noWorkerList;
			},

			// 获取用户信息
			getStudy() {
				var _this = this;
				_this.$u.get('/api/user.message/workStudy', {}).then(res => {
					uni.hideLoading();
					_this.studyList = res.data;
					if (res.data.length > 0) {
						_this.guideShow = true;
					}
				}).catch(res => {
					console.log(res);
				})
			},

			// 获取用户信息
			getUserInfo() {
				var _this = this;
				_this.$u.get('/api/user.index/getDetail', {}).then(res => {
					uni.hideLoading();
					console.log("==获取用户信息==");
					console.log(res);
					_this.userInfo = res.data;
				}).catch(res => {
					console.log(res);
				})
			},

			// 获取记工偏差
			getCheckCompare() {
				var _this = this;
				_this.$u.get('/api/project.compare/checkCompare', {
					project_id: _this.defaultWorksite.project_id,
					type: '1', //1记工、2记账
				}).then(res => {
					uni.hideLoading();
					console.log("==获取记工偏差==");
					console.log(res);
					var resData = res.data;
					if (resData > 0) {
						_this.isError = true;
					} else {
						_this.isError = false;
					}
				}).catch(res => {
					console.log(res);
				})
			},

			/**
			 * 日历 start
			 */
			// 获取日历组件的年
			getYear(value) {
				// console.log("获取日历组件的年：" + value)
				this.currentYear = value;
			},

			// 获取日历组件的月
			getMonth(value) {
				// console.log("获取日历组件的月：" + value)
				this.currentMonth = value;
			},

			// 获取日历组件的日
			getDay(value) {
				// console.log("获取日历组件的日：" + value)
				this.currentDay = value;
			},

			// 获取日历组件的模式
			getWeekMode(value) {
				// console.log("获取日历组件的模式：" + value)
				this.weekMode = value;
			},

			// 获取日历标记数据
			getDotList() {
				var _this = this;
				var month = '';
				if (_this.currentYear) {
					month = _this.currentYear + '-' + _this.currentMonth;
				}

				_this.$u.get('/api/project.monthdata/getMonthData', {
					project_id: _this.defaultWorksite.project_id,
					month: month,
				}).then(res => {
					uni.hideLoading();
					console.log('获取记工数据数据');
					console.log(res.data);
					_this.dotList = res.data;
					uni.setStorageSync('dotList',  res.data);
					
					//_this.getNoWorker();
					// 更新日历组件数据
					_this.$nextTick(() => {
						var date = new Date(); // 获取现在时间
						var nowYear = date.getFullYear() // 现在年份
						var nowMonth = date.getMonth() + 1; // 现在月份
						var nowDate = date.getDate() // 现在日期
						if (_this.currentYear) {
							if (_this.currentYear == nowYear && _this.currentMonth == nowMonth && _this
								.currentDay == nowDate) {
								_this.$refs.calendar.initDate();
							} else {
								if (_this.weekMode) {
									const d = new Date(_this.currentYear, _this.currentMonth - 1, _this
										.currentDay);
									_this.$refs.calendar.initDate(d);
								} else {
									const d = new Date(_this.currentYear, _this.currentMonth - 1, 1);
									_this.$refs.calendar.initDate(d);
								}
							}
						} else {
							_this.$refs.calendar.initDate();
						}
						this.$forceUpdate();
					})
				}).catch(res => {
					console.log(res);
				})
			},

			// 日历选中日期事件回调
			dateChange(e) {
				console.log(e);
				var _this = this;
				_this.date = e.fullDate;

				_this.getNoWorker();
				_this.noSelectAllStatus = false;
				_this.noSelectNum = 0;
				_this.getRecordList();

			},

			// 获取未记工的工人
			getNoWorker() {
				var _this = this;
				_this.$u.get('/api/project.member/dayMember', {
					project_id: _this.defaultWorksite.project_id,
					days: _this.date,
				}).then(res => {
					uni.hideLoading();
					console.log("==获取未记工的工人==");
					console.log(res.data);
					var noWorkerList = res.data;
					for (var i = 0; i < noWorkerList.length; i++) {
						// 带班默认不勾选
						if (_this.defaultWorksite.role == 2) {
							noWorkerList[i].selected = 0;
						} else if (_this.defaultWorksite.role == 1) {
							// 工人默认勾选自己
							noWorkerList[i].selected = 1;
							_this.noSelectNum = 1;
						}
						noWorkerList[i].work_type = 0; //0点工，1包工
					}

					_this.noWorkerList = noWorkerList;
					_this.noWorkerTotal = noWorkerList.length;
					if (_this.noWorkerTotal == 0) {
						_this.recordTabIndex = 2
					}
					if (noWorkerList.length > 0) {
						_this.recordTabIndex = 1;
					}
					// 
					// 缓存未记工的工人
					uni.setStorageSync('noWorkerList', JSON.stringify(noWorkerList));
				}).catch(res => {
					console.log(res);
				})
			},

			// 工人单选
			noWorkerSelect(index) {
				var _this = this;
				var noWorkerList = _this.noWorkerList;
				
				// if(noWorkerList[index].is_lock==1){
				// 	uni.showModal({
				// 		title:'数据锁定',
				// 		content:'当前月份数据已经签字，修改前必须先撤销签字，是否去撤销签字？',
				// 		success(res){
				// 			if(res.confirm){
								
				// 				uni.navigateTo({
				// 					url:'/pages/wage/wageHome?member_id='+noWorkerList[index].id+'&project_id='+noWorkerList[index].project_id+'&currentyear='+_this.currentYear+'&currentmonth='+_this.currentMonth
				// 				})
				// 			}
				// 		}
				// 	})
				// 	return;
				// }
				if (_this.recordTabIndex == 2) {
					_this.recordTabIndex = 1
				}
				// 选择之前，需要把当前选择的并且保存记工的数据设置为2，没有记工的设置为0
				for (var i = 0; i < noWorkerList.length; i++) {
					if (noWorkerList[i].selected == 1 && noWorkerList[i].num == 0) {
						noWorkerList[i].selected = 1;
						noWorkerList[i].num = 0;
						noWorkerList[i].overtime_hour = 0;
						noWorkerList[i].overtime_days = 0;
						noWorkerList[i].way_type = 1;
					}
					if (noWorkerList[i].selected == 1 && (noWorkerList[i].num > 0 || noWorkerList[i].num == '休')) {
						noWorkerList[i].selected = 2;
					}
				}

				// 设置当前选中的状态
				if (noWorkerList[index].selected == 1 && noWorkerList[index].num == 0) {
					console.log('==选中但是未记工==')
					noWorkerList[index].selected = 0;
					noWorkerList[index].num = 0;
					noWorkerList[index].overtime_hour = 0;
					noWorkerList[index].overtime_days = 0;

				} else if (noWorkerList[index].selected == 1 && (noWorkerList[index].num > 0 || noWorkerList[index].num ==
						'休')) {
					noWorkerList[i].selected = 2;
				} else if (noWorkerList[index].selected == 2) {
					noWorkerList[index].selected = 0;
					noWorkerList[index].num = 0;
					noWorkerList[index].overtime_hour = 0;
					noWorkerList[index].overtime_days = 0;
					// noWorkerList[index].way_type = 1;
					_this.workType = 0;
					_this.workNum = -1; //提交的工
				} else if (noWorkerList[index].selected == 1 && noWorkerList[index].num == -1) { //全选，取消权限
					noWorkerList[index].selected = 0;
					noWorkerList[index].num = 0;
					noWorkerList[index].overtime_hour = 0;
					noWorkerList[index].overtime_days = 0;
				} else {
					noWorkerList[index].selected = 1;

				}
				noWorkerList[index].way_type = 1;

				_this.workType = 0;
				_this.workNum = -1; //提交的工
				_this.workInput = ''; //提交的工
				_this.overWork = 0; //提交的工
				_this.selectOver = 1; //提交的工

				// 判断是否达到全选
				var noSelectNum = 0;
				for (var i = 0; i < noWorkerList.length; i++) {
					if (noWorkerList[i].selected > 0) {
						noSelectNum++;
					}
				}
				if (noSelectNum == noWorkerList.length) {
					_this.noSelectAllStatus = true;
				} else {
					_this.noSelectAllStatus = false;
				}

				_this.noWorkerList = noWorkerList;
				_this.noSelectNum = noSelectNum;

				// 缓存未记工的工人
				uni.setStorageSync('noWorkerList', JSON.stringify(noWorkerList));
			},

			// 全选
			noSelectAll() {
				var _this = this;
				var noSelectAllStatus = _this.noSelectAllStatus;
				noSelectAllStatus = !noSelectAllStatus;
				var noWorkerList = _this.noWorkerList;
				// for (let i = 0; i < noWorkerList.length; i++) {
				// 	if(noWorkerList[i].is_lock==1){
				// 		uni.showModal({
				// 			title:'数据锁定',
				// 			content:'当前月份数据已经签字，修改前必须先撤销签字，是否去撤销签字？',
				// 			success(res){
				// 				if(res.confirm){
									
				// 					uni.navigateTo({
				// 						url:'/pages/wage/wageHome?member_id='+noWorkerList[i].id+'&project_id='+noWorkerList[i].project_id
				// 					})
				// 				}
				// 			}
				// 		})
				// 		return;
				// 	}
				// }
				
				
				for (let i = 0; i < noWorkerList.length; i++) {
					noWorkerList[i].selected = noSelectAllStatus;
					
					noWorkerList[i].num = -1;
					noWorkerList[i].overtime_hour = 0;
					noWorkerList[i].overtime_days = 0;
					_this.workType = 0;
					_this.selectOver = 1;
					_this.overWork = 0;
					//}

				}

				// 判断是否达到全选
				var noSelectNum = 0;
				for (var i = 0; i < noWorkerList.length; i++) {
					if (noWorkerList[i].selected == true) {
						noSelectNum++;
					}
				}
				if (noSelectNum == noWorkerList.length) {
					_this.noSelectAllStatus = true;
				} else {
					_this.noSelectAllStatus = false;
				}

				_this.noWorkerList = noWorkerList;
				_this.noSelectNum = noSelectNum;
				_this.recordTabIndex = 1;

				// 缓存未记工的工人
				uni.setStorageSync('noWorkerList', JSON.stringify(noWorkerList));
			},

			/**
			 * 获取记工数据
			 */
			getRecordList() {
				var _this = this;
				_this.$u.get('/api/project.work/getDayList', {
					project_id: _this.defaultWorksite.project_id,
					add_date: _this.date,
				}).then(res => {
					uni.hideLoading();
					console.log("==获取记工数据==");
					console.log(res);
					var recordList = res.data;
					_this.recordList = recordList;
					_this.recordTotal = recordList.length;
					_this.totalTodayMoney = res.extend.totalMoney
					_this.totalTodayNum = res.extend.totalNum,
						_this.extendData = res.extend;
				}).catch(res => {
					console.log(res);
				})
			},

			//查看全部记功数据
			allWork(memberId, projectId) {
				uni.navigateTo({
					//url:'/pages/record/recordStatistics/recordStatistics?member_id='+memberId+'&project_id='+projectId
					url: '/pages/worker/workerDetail/workerDetail?id=' + memberId
				})
			},

			// 页面跳转
			goPage(url) {
				uni.navigateTo({
					url: url
				})
			},

			/**
			 * 引导弹窗
			 */
			stepClick() {
				this.studyIndex++;
			},

			// 打开上传图片
			openUpload() {
				var _this = this;
				// #ifdef APP-PLUS
				uni.showActionSheet({
					itemList: ['摄像机',  '相册选择'],
					success: function(res) {
						if (res.tapIndex == 0) {
							_this.isUploadCamera = true;
						}
						if (res.tapIndex == 1) {
							uni.navigateTo({
								url: '/pages/watermarkCamera/imgCamera'
							})
						}
						if (res.tapIndex == 2) {
							_this.isUpload = true;
						}
					},
					fail: function(res) {
						console.log(res.errMsg);
					}
				});
				return
				// #endif
				uni.showActionSheet({
					itemList: ['摄像机', '水印相机', '相册选择'],
					success: function(res) {
						if (res.tapIndex == 0) {
							_this.isUploadCamera = true;
						}
						if (res.tapIndex == 1) {
							uni.navigateTo({
								url: '/pages/watermarkCamera/imgCamera'
							})
						}
						if (res.tapIndex == 2) {
							_this.isUpload = true;
						}

					},
					fail: function(res) {
						console.log(res.errMsg);
					}
				});

			},

			// 获取上传的图片
			getImgsFunc(e) {
				let _this = this;
				if (e && typeof(e) != 'undefined') {
					_this.imgList = _this.imgList.concat(e);
				}
				_this.isUpload = false;
				_this.isUploadCamera = false;
			},

			// 预览图片
			previewImg(src) {
				let _this = this;
				// console.log(_this.imgList);
				var current = src;
				var imgList = _this.imgList;
				var imagesUrl = [];
				for (var i = 0; i < imgList.length; i++) {
					imagesUrl[i] = imgList[i].url;
				}
				// console.log(imagesUrl)
				uni.previewImage({
					current: current,
					urls: imagesUrl
				})
			},

			// 图片删除
			deleteImg(index) {
				// console.log(index);
				let _this = this;
				_this.imgList.splice(index, 1);
			},
			
			// 重置表单
			clearData() {
				var _this = this;
				_this.selectType = 1;
				_this.workType = 0; //上班
				_this.workNum = -1; //提交的工
				_this.workInput = '';  //输入的工
				_this.overWork = ''; //加班
				_this.selectOver = '';
				_this.remark = '';
				_this.imgList = [];
			},

		}
	}
</script>

<style>
	page {
		background-color: #fff;
	}
</style>
<style scoped lang="scss">
	/* 日历组件 */
	.calendarBox {}

	/* 选择工友 */
	.recordBox {
		padding: 0 0 0 30rpx;
		background-color: $uni-color-white;
		overflow: hidden;
		margin: 20rpx 0;
	}

	.recordHead_con {
		padding: 30rpx 30rpx 30rpx 0;
		overflow: hidden;
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.recordNum {
		font-size: 32rpx;
		font-weight: bold;
		color: #000;
	}

	.recordNum text {
		font-size: 28rpx;
		font-weight: normal;
		margin-left: 10rpx;
	}

	.recordAll {
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.recordAll text {
		margin-left: 5rpx;
	}

	.recordBody_con {
		overflow: hidden;
	}

	/* 工人 */
	.workerList_con {
		overflow: hidden;
		display: flex;
		flex-wrap: wrap;
	}

	.workerItem {
		margin-bottom: 30rpx;
		margin-right: 30rpx;
	}

	.workerItem:nth-child(6n) {
		// margin-right: 0;
	}

	.workerAvatar {
		width: 90rpx;
		height: 90rpx;
		font-size: 28rpx;
		font-weight: bold;
		color: $uni-color-white;
		box-sizing: border-box;
		border-radius: 10rpx;
		background: $uni-color-active;
		display: flex;
		justify-content: center;
		align-items: center;
		position: relative;
	}

	.workerAvatar .workerTick {
		width: 90rpx;
		height: 90rpx;
		background: url('/static/keep/workerTick.png') no-repeat center center;
		background-size: cover;
		overflow: hidden;
		position: absolute;
		left: 0;
		top: 0;
		z-index: 10;
	}

	.workerAvatar .workerSet {
		width: 100%;
		height: 30rpx;
		line-height: 30rpx;
		text-align: center;
		color: $uni-color;
		font-size: 20rpx;
		background: url('/static/keep/workerSet.png') no-repeat center center;
		background-size: cover;
		overflow: hidden;
		position: absolute;
		left: 0;
		bottom: 0;
		z-index: 20;
	}

	.workerName {
		text-align: center;
		font-size: 26rpx;
		color: $uni-color;
		overflow: hidden;
		margin-top: 20rpx;
	}

	/* 记工流水 */
	.recordHistory_box {
		padding-bottom: 60rpx;
	}

	.historyHead_con {
		padding: 30rpx;
		border-bottom: 1rpx solid #eee;
		overflow: hidden;
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.historyDate {
		font-size: 28rpx;
		color: $uni-color;
	}

	.historyStats {
		display: flex;
		align-items: center;
	}

	.historyStats .statsItem {
		margin-right: 30rpx;
		display: flex;
		align-items: center;
	}

	.historyStats .statsItem:last-child {
		margin-right: 0;
	}

	.historyStats .statsType {
		width: 32rpx;
		height: 32rpx;
		line-height: 32rpx;
		text-align: center;
		color: #8B8A90;
		font-size: 24rpx;
		background: #f0f0f0;
		border-radius: 5rpx;
		overflow: hidden;
	}

	.historyStats .statsNum {
		font-size: 28rpx;
		color: $uni-color;
		margin-left: 20rpx;
	}

	.recordList_con {
		padding: 0 30rpx;
	}

	.recordList_conPop {
		max-height: 800rpx;
		overflow-y: scroll;
	}

	.recordItem {
		padding: 30rpx 0;
		border-bottom: 2rpx solid #f5f5f5;
		overflow: hidden;
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.recordItemPop image {
		width: 40rpx;
		height: 40rpx;
		padding: 20rpx;
		border-radius: 20rpx;
		background-color: $uni-color-active;
	}

	.recordCon {
		flex: 2;
		font-size: 32rpx;
		overflow: hidden;
		margin: 0 20rpx;
	}

	.recordAvatar {
		width: 80rpx;
		height: 80rpx;
		border-radius: 10rpx;
		overflow: hidden;
	}

	.recordAvatar image {
		width: 100%;
		height: 100%;
	}

	.recordName {
		font-size: 32rpx;
	}

	.recordName .modify {
		font-weight: 400;
		font-size: 22rpx;
		color: $uni-color-warning;
		padding: 0 10rpx;
		border: 2rpx solid $uni-color-warning;
		border-radius: 14rpx 2rpx 14rpx 0;
		margin-left: 30rpx;
		margin-top: -8rpx;
	}

	.recordName .recorder {
		font-weight: 400;
		font-size: 26rpx;
		color: $uni-color-active;
		padding-left: 10rpx;
	}

	.recordType {
		font-size: 24rpx;
		color: $uni-color-text;
		margin-top: 10rpx;
	}

	.recordNote {
		width: 100%;
		font-size: 24rpx;
		color: #999;
		overflow: hidden;
		text-overflow: ellipsis;
		white-space: nowrap;
		margin-top: 10rpx;
	}

	.recordWork {
		text-align: center;
		overflow: hidden;
		margin-right: 20rpx;
	}

	.recordMoney {
		font-size: 32rpx;
		font-weight: bold;
		color: #000000;
		overflow: hidden;
	}

	.recordDay {
		font-size: 22rpx;
		color: $uni-color;
		overflow: hidden;
		// margin-top: 10rpx;
	}

	.recordEdit {
		width: 90rpx;
		height: 50rpx;
		line-height: 50rpx;
		text-align: center;
		font-size: 24rpx;
		color: $uni-color-text;
		border: 2rpx solid $uni-color-text;
		border-radius: 10rpx;
		box-sizing: border-box;
		overflow: hidden;

	}

	//数据确认弹出框
	.addUserBox {
		width: 100%;
		padding: 20rpx 20rpx 0 20rpx;
	}

	.addUserTitle {
		width: 100%;
		height: 80rpx;
		line-height: 80rpx;
		text-align: center;
		font-size: 28rpx;
		font-weight: bold;
	}
	
	.handleFixed{
		z-index: 99999999;
	}
	.handlePost_con{
		padding: 80rpx 0 30rpx 0;
	}
	
	
	.workNotice{
		color: #666;
		line-height: 50rpx;
	}
	.workNotice text{
		color: #327FE8;
	}
	
	.handleList_con .textareaCon textarea{
		height: 260rpx;
		font-size: 36rpx;
	}
	
	.addressBox{
		padding: 20rpx 0 30rpx 0;
		color: #999;
	}
</style>
